package com.qcby.mycommunity_003.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcby.mycommunity_003.annotation.LogAnnotation;
import com.qcby.mycommunity_003.entity.Camera;
import com.qcby.mycommunity_003.entity.User;
import com.qcby.mycommunity_003.mapper.CameraMapper;
import com.qcby.mycommunity_003.service.CameraService;
import com.qcby.mycommunity_003.util.Result;
import com.qcby.mycommunity_003.vo.CameraListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-03-28
 */
@RestController
@RequestMapping("/sys/camera")
public class CameraController {
    @Autowired
    private CameraService cameraService;
   @Autowired
   private CameraMapper cameraMapper;
    /**
     * 列表
     * @param commuityId
     * @return
     */
    @GetMapping("/list/{communityId}")
        public Result CameraList(@PathVariable("communityId") Integer commuityId){
        ArrayList<CameraListVo> list=new ArrayList<>();
        list=cameraService.CameraList(commuityId);
        return Result.ok().put("data",list);
    }

    /**
     * 添加摄像头
     * @param cameraListVo
     * @param session
     * @return
     */
    @LogAnnotation("添加摄像头")
    @PostMapping("/add")
    public Result InsertCamera(@RequestBody CameraListVo cameraListVo, HttpSession session){
       //通过session获取操作人
        User user = (User) session.getAttribute("user");
        Camera camera=new Camera();
        //插入
        BeanUtils.copyProperties(cameraListVo,camera);
        //防止覆盖
        camera.setCreater(user.getUsername());
        camera.setCameraId(cameraListVo.getCameraInfoId());
        camera.setMacId(cameraListVo.getMacID());
        cameraService.save(camera);
        return Result.ok();
    }

    /**
     * restful风格，回显
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result SelectById(@PathVariable("id") Integer id){
        CameraListVo cameraListVo=new CameraListVo();
        Camera camera = cameraService.getById(id);
        BeanUtils.copyProperties(camera,cameraListVo);
        cameraListVo.setCameraInfoId(camera.getCameraId());
        cameraListVo.setMacID(camera.getMacId());
        return Result.ok().put("data",cameraListVo);
    }

    /**
     * 修改摄像头
     * @param cameraListVo
     * @return
     */
    @LogAnnotation("编辑摄像头")
    @PutMapping("/edit")
    public Result updateCamera(@RequestBody CameraListVo cameraListVo){
        //条件构造器
        QueryWrapper<Camera> cameraQueryWrapper=new QueryWrapper<>();
        cameraQueryWrapper.eq("camera_id",cameraListVo.getCameraInfoId());
        //复制属性到camera
        Camera camera=new Camera();
        BeanUtils.copyProperties(cameraListVo,camera);
        camera.setMacId(cameraListVo.getMacID());
        camera.setCameraId(cameraListVo.getCameraInfoId());
        cameraService.update(camera,cameraQueryWrapper);
        return Result.ok();
    }

    /**
     * 删除摄像头
     * @param ids
     * @return
     */
    @LogAnnotation("删除摄像头")
    @DeleteMapping("/del")
    public Result delCamera(@RequestBody List<Integer> ids){
        int i = cameraMapper.deleteBatchIds(ids);
        if(i==0){return Result.error("删除失败");}
        return Result.ok();
    }
}

